<?php


namespace api\blog\controller;


use Qiniu\Auth;
use Qiniu\Storage\UploadManager;

class Qiniu
{
    //上传图片
    public function uploadImage(){
        // 用于签名的公钥和私钥
        $accessKey = config('app.qiniuOss.accessKey');
        $secretKey = config('app.qiniuOss.secretKey');
        $bucket = config('app.qiniuOss.bucket');
        $file = request()->file('file');
        //原文件名
        $oldfile = $file->getOriginalExtension();
        //截取后缀名
        $ext = strchr($oldfile, '.');
        //重命名
        $newName = time().mt_rand(111,999).$ext;
        // 构建鉴权对象
        $auth = new Auth($accessKey,$secretKey);
        //生成上传Token
        $token = $auth->uploadToken($bucket);
        //构建UploadManager对象
        $uploadMgr = new UploadManager();
        //调用UploadManager的putFile方法进行文件的上传
        list($ret, $err) = $uploadMgr->putFile($token, $newName, $file->getPathname(), null, 'application/octet-stream', true, null, 'v2');
        if ($err !== null) {
            var_dump($err);
        } else {
            //var_dump($ret);
            return json(['code'=>1,'msg'=>'ok','data'=>[
                'url' => 'http://sejtwnz58.hb-bkt.clouddn.com/'.$ret['key'],
            ]]);
        }
    }
}